package com.xieman.anti.replay.utils;


import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.lang.reflect.Method;

public class SpringUtils {

    public static Method getMethod(ProceedingJoinPoint point) {
        try {
            Signature signature = point.getSignature();
            MethodSignature ms = (MethodSignature) signature;
            return point.getTarget().getClass().getMethod(ms.getName(),ms.getParameterTypes());
        } catch (NoSuchMethodException e) {
            throw new com.xieman.anti.replay.exception.NoSuchMethodException("获取方法失败",e);
        }
    }

    public static String getMethodName(ProceedingJoinPoint point) {
        return getClass(point).getName() + '.' + getMethod(point).getName();
    }

    public static Class<?> getClass(ProceedingJoinPoint point) {
        return point.getTarget().getClass();
    }

    public static HttpServletRequest getHttpServletRequest() {
        return getServletRequestAttributes().getRequest();
    }

    public static ServletRequestAttributes getServletRequestAttributes() {
        return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    }
}
